#
# Copyright 2009 Red Hat, Inc. and/or its affiliates.
# 
# Licensed to you under the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  See the files README and
# LICENSE_GPL_v2 which accompany this distribution.
#
"""
Cache module provides general purpose (more or less) cache infrastructure
for keeping storage related data that is expensive to harvest, but needed often
"""

class Cache:
    """
    Cache class provides interface to the general purpose object cache.
    The main characteristic of the cache is that it keeps the many objects as
    a single entity. Whenever the cache expires the whole content is being
    discarded.

    #### NOT USED #####
    autoflush - whether to allow automatic flush of cache content when
                      it is aged beyond the expiration
              True = allow autoflush
		      False = disallow autoflush; the content will be kept until
                      manually flushed. However the stale data wouldn't
                      be served
    """
    def __init__(self):
        self.__objects = {}

    def add(self, key, value):
        """
        Add new item to the cache
        """
        # FIXME: check that key does not already exist in cache
        self.__objects[key] = value

    def remove(self, key):
        """
        Remove the particular item from the cache
        """
        if self.__objects.has_key(key):
            del self.__objects[key]

    def lookup(self, key):
        """
        Lookup the object in the cache by its key
        """
        return self.__objects.get(key)

    def getvalues(self):
        """
        Export the entire content of the cache as a list
        """
        return self.__objects.values()

    def getkeys(self):
        """
        Export all the keys of the cache content as a list
        """
        return self.__objects.keys()

    def setall(self, d):
        """
        Import an external dictionary as a cache content
        """
        self.__objects = d

    def flush(self):
        """
        Flush the entire cache
        """
        self.__objects.clear()

def main():
    cache = Cache()
    cache.add(1, 1)

if __name__ == "__main__":
    main()
